package cn.gudian.cms.Message

import cn.gudian.cms.utils.PinYin

class LinkManController {

  def index = { redirect(action: "list", params: params) }

  // the delete, save and update actions only accept POST requests
  static allowedMethods = [save: "POST", update: "POST", delete: "POST"]

  def list = {
    params.max = Math.min(params.max ? params.max.toInteger() : 10, 100)
    [linkManInstanceList: LinkMan.list(params), linkManInstanceTotal: LinkMan.count()]
  }

  def create = {
    def linkManInstance = new LinkMan()
    linkManInstance.properties = params
    return [linkManInstance: linkManInstance]
  }

  def save = {
    def linkManInstance = new LinkMan(params)
    linkManInstance.pinyin = PinYin.getFirstSpell(params?.name) + "," + PinYin.getSpell(params?.name)
    if (!linkManInstance.hasErrors() && linkManInstance.save()) {
     render "<script>ownerDialog.close();</script>"
    }
    else {
      render(view: "create", model: [linkManInstance: linkManInstance])
    }
  }

  def show = {
    def linkManInstance = LinkMan.get(params.id)
    if (!linkManInstance) {
      flash.message = "linkMan.not.found"
      flash.args = [params.id]
      flash.defaultMessage = "LinkMan not found with id ${params.id}"
      redirect(action: "list")
    }
    else {
      return [linkManInstance: linkManInstance]
    }
  }

  def edit = {
    def linkManInstance = LinkMan.get(params.id)
    if (!linkManInstance) {
      flash.message = "linkMan.not.found"
      flash.args = [params.id]
      flash.defaultMessage = "LinkMan not found with id ${params.id}"
      redirect(action: "list")
    }
    else {
      return [linkManInstance: linkManInstance]
    }
  }

  def update = {
    def linkManInstance = LinkMan.get(params.id)
    if (linkManInstance) {
      if (params.version) {
        def version = params.version.toLong()
        if (linkManInstance.version > version) {

          linkManInstance.errors.rejectValue("version", "linkMan.optimistic.locking.failure", "Another user has updated this LinkMan while you were editing")
          render(view: "edit", model: [linkManInstance: linkManInstance])
          return
        }
      }
      linkManInstance.properties = params
      linkManInstance.pinyin = PinYin.getFirstSpell(params?.name) + "," + PinYin.getSpell(params?.name)
      if (!linkManInstance.hasErrors() && linkManInstance.save()) {
        flash.message = "linkMan.updated"
        flash.args = [params.id]
        flash.defaultMessage = "LinkMan ${params.id} updated"
        redirect(action: "show", id: linkManInstance.id)
      }
      else {
        render(view: "edit", model: [linkManInstance: linkManInstance])
      }
    }
    else {
      flash.message = "linkMan.not.found"
      flash.args = [params.id]
      flash.defaultMessage = "LinkMan not found with id ${params.id}"
      redirect(action: "edit", id: params.id)
    }
  }

  def delete = {
    def linkManInstance = LinkMan.get(params.id)
    if (linkManInstance) {
      try {
        linkManInstance.delete()
        flash.message = "linkMan.deleted"
        flash.args = [params.id]
        flash.defaultMessage = "LinkMan ${params.id} deleted"
        redirect(action: "list")
      }
      catch (org.springframework.dao.DataIntegrityViolationException e) {
        flash.message = "linkMan.not.deleted"
        flash.args = [params.id]
        flash.defaultMessage = "LinkMan ${params.id} could not be deleted"
        redirect(action: "show", id: params.id)
      }
    }
    else {
      flash.message = "linkMan.not.found"
      flash.args = [params.id]
      flash.defaultMessage = "LinkMan not found with id ${params.id}"
      redirect(action: "list")
    }
  }

  def deleteAll = {
    def qstr = params.qstr
    if (qstr) {
      try {
        LinkMan.executeUpdate("delete LinkMan where id in(" + qstr + ")")
        flash.message = "delete successfully:" + qstr
        redirect(action: "list")
      } catch (Exception e) {
        flash.message = "delete failed:" + qstr
        redirect(action: "list")
      }
    }
  }
}
